package com.mvd.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kongzue.dialog.v3.MessageDialog;
import com.kongzue.dialog.v3.TipDialog;
import com.kongzue.dialog.v3.WaitDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mvd.app.R;
import com.mvd.app.application.BaseApplication;
import com.mvd.app.commons.JsonUtil;
import com.mvd.app.commons.PDALogUtils;
import com.mvd.app.commons.SharedPreferencesUtils;
import com.mvd.app.commons.UtilsLog;
import com.mvd.app.model.TianJin22Model;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.darsh.multipleimageselect.helpers.Constants.REQUEST_CODE;

public class TianJingQRcodeActivity extends BaseActivity {
    public TianJingQRcodeActivity instances = null;
    @BindView(R.id.ac_qrcode_saomiao)
    Button acQrcodeSaomiao;
    @BindView(R.id.ac_qrcode_number)
    TextView acQrcodeNumber;
    @BindView(R.id.ac_qrcode_id)
    TextView acQrcodeId;
    @BindView(R.id.ac_qrcode_haopai_type)
    TextView acQrcodeHaopaiType;
    @BindView(R.id.ac_qrcode_dajiahao)
    TextView acQrcodeDajiahao;
    @BindView(R.id.ac_qrcode_)
    TextView acQrcode;
    @BindView(R.id.ac_qrcode_jiancezhan_code)
    TextView acQrcodeJiancezhanCode;
    @BindView(R.id.ac_qrcode_qiandao)
    Button acQrcodeQiandao;
    TianJin22Model tianJin22Model = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_qrcode);
        ButterKnife.bind(this);
        setBackBtn();
        setTopTitle("扫码签到");
        String logStrf = "\n" + "onCreate-----扫码签到---" + "\n";
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStrf.getBytes());
        instances = this;
        ZXingLibrary.initDisplayOpinion(this);
    }

    @OnClick({R.id.ac_qrcode_saomiao, R.id.ac_qrcode_qiandao})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ac_qrcode_saomiao:
                Intent intent = new Intent(this, CaptureActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
//                customScan();
//                Intent intent=new Intent(this,QRScanActivity.class);
//                startActivity(intent);
                break;
            case R.id.ac_qrcode_qiandao:
//                tianjian_qiandao("1111111111","2222222222");
                if (tianJin22Model != null) {
                    tianjian_qiandao(tianJin22Model.getData().getCP(), tianJin22Model.getData().getID());
                }
//                customScan();
//                Intent intent=new Intent(this,QRScanActivity.class);
//                startActivity(intent);
                break;
        }
    }

    @Override
// 通过 onActivityResult的方法获取 扫描回来的 值
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String logStrf = "\n" + "onCreate-----扫码签到---" + "\n";
        /**
         * 处理二维码扫描结果
         */
        if (requestCode == REQUEST_CODE) {
            //处理扫描结果（在界面上显示）
            if (null != data) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                    String result = bundle.getString(CodeUtils.RESULT_STRING);
                    if (!TextUtils.isEmpty(result)) {
                        tianjian_qrcode(result);
                    }
//                        Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();
                    UtilsLog.e("扫描结果：" + result);
                    logStrf = logStrf + "扫描结果:" + result + "\n";
                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                    Toast.makeText(instances, "解析二维码失败", Toast.LENGTH_LONG).show();
                    logStrf = logStrf + "解析二维码失败" + "\n";
                }
                PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStrf.getBytes());
            }
        }
    }

    private void tianjian_qrcode(String code) {
        QRCode model = new QRCode();
        model.setMsg(code);
//        String jsonStr="{\"msg:\""+code+"\"}";
        Gson gson = new Gson();
        String jsonStr = gson.toJson(model);
        String logStr = "\n" + "天津扫码-----json" + "\n" + "json::" + jsonStr + "\n" +
                "URL::" + SharedPreferencesUtils.getIP(instances) + TIANJIN_QRCODE;
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
        UtilsLog.e("天津扫码----jsonStr==" + jsonStr);
        UtilsLog.e("天津扫码----url==" + SharedPreferencesUtils.getIP(this) + TIANJIN_QRCODE);
        OkGo.<String>post(SharedPreferencesUtils.getIP(this) + TIANJIN_QRCODE)
                .tag(this)
                .upJson(jsonStr)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
//                        CommonUtils.showLoadingDialog(instances, "加载中...");
                        WaitDialog.show(instances, "请稍候...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        WaitDialog.dismiss();
                        String result = response.body().trim();
                        UtilsLog.e("天津扫码----result==" + result);
//                        UtilsLog.e("天津扫码----result=22=" + result.substring(1, result.length() -
//                                1));
//                        String newResult = result.substring(1, result.length() - 1).replaceAll("\\\\", "");
                        String logStr = "\n" + "天津扫码---请求成功" + "\n" + "result::" + result + "\n";
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
//                        CommonUtils.hideLoadingDialog(instances);


                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            int code = jsonObject.getInt("code");
                            if (code == 200) {
                                tianJin22Model = JsonUtil.fromJson(result, TianJin22Model.class);
                                if (tianJin22Model != null) {
                                    acQrcodeNumber.setText(tianJin22Model.getData().getCP());
                                    acQrcodeId.setText(tianJin22Model.getData().getID());
                                    acQrcodeHaopaiType.setText(tianJin22Model.getData().getHP());
                                    acQrcodeDajiahao.setText(tianJin22Model.getData().getCJ());
                                    acQrcode.setText(tianJin22Model.getData().getRQ() + "  " + tianJin22Model.getData().getSJ());
                                    acQrcodeJiancezhanCode.setText(tianJin22Model.getData().getJG());
                                }
                            } else {
                                Toast.makeText(instances, jsonObject.getString("msg"), Toast.LENGTH_LONG).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
//                        if ("ok".equalsIgnoreCase(str)) {
//                            TipDialog.show(instances, "请求成功", TipDialog.TYPE.SUCCESS);
//                        } else {
//                            MessageDialog.show(instances, "提示", "指标核查请求失败，请重试", "确定");
//                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        WaitDialog.dismiss();
                        String logStr2 = "\n" + "天津扫码----onError" + "\n" +
                                "result::" + response.body();
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr2.getBytes());
                        UtilsLog.e("天津扫码----onError==" + response.body());
//                        CommonUtils.hideLoadingDialog(instances);
                        MessageDialog.show(instances, "提示", "扫描二维码异常，请重试", "确定");
                    }
                });
    }


    private void tianjian_qiandao(String v1, String v2) {
        QianDao model = new QianDao();
        model.setHphm(v1);
        model.setOrder_id(v2);
//        String jsonStr="{\"msg\":"+code+"}";
        Gson gson = new Gson();
        String jsonStr = gson.toJson(model);
        String logStr = "\n" + "天津签到-----json" + "\n" + "json::" + jsonStr + "\n" +
                "URL::" + SharedPreferencesUtils.getIP(instances) + TIANJIAN_QIANDAO;
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
        UtilsLog.e("天津签到----jsonStr==" + jsonStr);
        UtilsLog.e("天津签到----url==" + SharedPreferencesUtils.getIP(this) + TIANJIAN_QIANDAO);
        OkGo.<String>post(SharedPreferencesUtils.getIP(this) + TIANJIAN_QIANDAO)
                .tag(this)
                .upJson(jsonStr)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
//                        CommonUtils.showLoadingDialog(instances, "加载中...");
                        WaitDialog.show(instances, "请稍候...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        WaitDialog.dismiss();
                        String result = response.body().trim();
                        UtilsLog.e("天津签到----result==" + result);
//                        UtilsLog.e("天津签到----result=22=" + result.substring(1, result.length() -
//                                1));
//                        String newResult = result.substring(1, result.length() - 1).replaceAll("\\\\", "");
                        String logStr = "\n" + "天津签到------请求成功" + "\n" + "result::" + result + "\n";
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
//                        CommonUtils.hideLoadingDialog(instances);
//                        try {
//                            JSONArray jsonArray = new JSONArray(newResult);
//                            String tip = jsonArray.getJSONObject(0).getJSONObject("result").getString("hcsm");
//                            Toast.makeText(instances, tip, Toast.LENGTH_LONG).show();
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
                        if ("ok".equalsIgnoreCase(result)) {
                            TipDialog.show(instances, "成功", TipDialog.TYPE.SUCCESS);
                        } else {
                            MessageDialog.show(instances, "提示", result, "确定");
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        WaitDialog.dismiss();
                        String logStr2 = "\n" + "天津签到----onError" + "\n" +
                                "result::" + response.body();
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr2.getBytes());
                        UtilsLog.e("天津签到----onError==" + response.body());
//                        CommonUtils.hideLoadingDialog(instances);
                        MessageDialog.show(instances, "提示", "扫描二维码异常，请重试", "确定");
                    }
                });
    }

    class QianDao {
        String hphm, order_id;

        public String getHphm() {
            return hphm;
        }

        public void setHphm(String hphm) {
            this.hphm = hphm;
        }

        public String getOrder_id() {
            return order_id;
        }

        public void setOrder_id(String order_id) {
            this.order_id = order_id;
        }

        @Override
        public String toString() {
            return "QianDao{" +
                    "hphm='" + hphm + '\'' +
                    ", order_id='" + order_id + '\'' +
                    '}';
        }
    }

    class QRCode {
        String msg;

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        @Override
        public String toString() {
            return "QRCode{" +
                    "msg='" + msg + '\'' +
                    '}';
        }
    }
}
